package com.example.cardgame

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {

    private val REQUEST_CODE_EDIT_INFO = 1001
    private val REQUEST_CODE_GAME = 1002

    private lateinit var nameInput: EditText
    private lateinit var genderInput: EditText
    private lateinit var studentIdInput: EditText
    private lateinit var phoneInput: EditText
    private lateinit var emailInput: EditText
    private lateinit var hobbyInput: EditText

    private lateinit var highScoreInput: EditText


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


        nameInput = findViewById(R.id.nameInput)
        genderInput = findViewById(R.id.gender)
        studentIdInput = findViewById(R.id.studentIdInput)
        phoneInput = findViewById(R.id.phoneInput)
        emailInput = findViewById(R.id.emailInput)
        hobbyInput = findViewById(R.id.hobbyInput)
        highScoreInput = findViewById(R.id.highScoreInput)

        val editInfoButton: Button = findViewById(R.id.editInfoButton)
        val startGameButton: Button = findViewById(R.id.startGameButton)


    editInfoButton.setOnClickListener {
            val intent = Intent(this, Activity2::class.java)
            startActivityForResult(intent, REQUEST_CODE_EDIT_INFO)
        }

        startGameButton.setOnClickListener {
            val intent = Intent(this, Activity3::class.java)
            startActivityForResult(intent, REQUEST_CODE_GAME)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_EDIT_INFO && resultCode == RESULT_OK) {

            data?.let {
                val name = it.getStringExtra("name")
                val studentId = it.getStringExtra("studentId")
                val phone = it.getStringExtra("phone")
                val email = it.getStringExtra("email")
                val gender = it.getStringExtra("gender")
                val hobbies = it.getStringArrayExtra("hobbies")?.joinToString(", ")

                nameInput.setText(name)
                genderInput.setText(gender)
                studentIdInput.setText(studentId)
                phoneInput.setText(phone)
                emailInput.setText(email)
                hobbyInput.setText(hobbies)
            }
        }else if(requestCode == REQUEST_CODE_GAME && resultCode == RESULT_OK) {
            data?.let {
                val highScore = it.getStringExtra("SCORE_KEY")
                Log.d("MainActivity", "Received high score: $highScore")
                highScoreInput.setText(highScore)
            }
        }
    }
}




